package gui.windows;

import gui.GUIHandler;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;

import room.Room;

import app.AppGlobals;

import config.ConfigHandler;
import config.LauncherHelper;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dialog.ModalExclusionType;

public class HostWindow {

	public JFrame frmHost;
	private JTextField textField;
	private JTextField textField_1;
	public Boolean rankedGame = false;
	/**
	 * Create the application.
	 */
	public HostWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmHost = new JFrame();
		frmHost.setModalExclusionType(ModalExclusionType.TOOLKIT_EXCLUDE);
		frmHost.setTitle("Host");
		frmHost.setBounds(100, 100, 323, 390);
		frmHost.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frmHost.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Settings", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 11, 287, 301);
		frmHost.getContentPane().add(panel);
		panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(10, 128, 267, 100);
		panel.add(panel_1);
		panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Additional Options", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setLayout(null);
		
		final JCheckBox chckbxEnablePriority = new JCheckBox("Enable Priority");
		chckbxEnablePriority.setBounds(6, 17, 97, 23);
		panel_1.add(chckbxEnablePriority);
		if(AppGlobals.launcherConfig.enablePrority) 
			chckbxEnablePriority.setSelected(true);
		
		final JCheckBox chckbxDisableCheckDeck = new JCheckBox("Disable Check Deck");
		chckbxDisableCheckDeck.setBounds(6, 43, 133, 23);
		panel_1.add(chckbxDisableCheckDeck);
		if(AppGlobals.launcherConfig.disableCheckDeck) 
			chckbxDisableCheckDeck.setSelected(true);
		
		final JCheckBox chckbxDisableShuffleDeck = new JCheckBox("Disable Shuffle Deck");
		chckbxDisableShuffleDeck.setBounds(6, 69, 133, 23);
		panel_1.add(chckbxDisableShuffleDeck);
		if(AppGlobals.launcherConfig.disableShuffleDeck) 
			chckbxDisableCheckDeck.setSelected(true);
		
		JLabel lblBanList = new JLabel("Ban List");
		lblBanList.setBounds(10, 28, 88, 14);
		panel.add(lblBanList);
		
		JLabel lblCardRules = new JLabel("Card Rules");
		lblCardRules.setBounds(10, 53, 88, 14);
		panel.add(lblCardRules);
		
		JLabel lblDuelMode = new JLabel("Duel Mode");
		lblDuelMode.setBounds(10, 78, 88, 14);
		panel.add(lblDuelMode);
		
		JLabel lblTimeLimit = new JLabel("Time Limit");
		lblTimeLimit.setBounds(10, 103, 88, 14);
		panel.add(lblTimeLimit);
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"2013.3.1", "2013.3.1 TF", "2012.9.1", "2012.3.1", "2011.9.1", "Traditional", "Unlimited", "Mega-Banned"}));
		comboBox.setBounds(109, 25, 168, 20);
		panel.add(comboBox);
		
		final JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"OCG", "TCG","OCG/TCG","Anime", "Turbo Duel"}));
		comboBox_1.setBounds(109, 50, 168, 20);
		comboBox_1.setSelectedIndex(2);
		panel.add(comboBox_1);
		
		final JComboBox comboBox_2 = new JComboBox();
		comboBox_2.setModel(new DefaultComboBoxModel(new String[] {"Single", "Match", "Tag"}));
		comboBox_2.setBounds(109, 75, 168, 20);
		panel.add(comboBox_2);
		
		final JComboBox comboBox_3 = new JComboBox();
		comboBox_3.setModel(new DefaultComboBoxModel(new String[] {"3 Minutes", "5 Minutes"}));
		comboBox_3.setBounds(109, 100, 168, 20);
		panel.add(comboBox_3);
		
		JLabel lblLifepoints = new JLabel("Lifepoints");
		lblLifepoints.setBounds(10, 239, 46, 14);
		panel.add(lblLifepoints);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(10, 264, 46, 14);
		panel.add(lblPassword);
		
		textField = new JTextField();
		textField.setText("8000");
		textField.setBounds(109, 236, 168, 20);
		panel.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(109, 261, 168, 20);
		panel.add(textField_1);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frmHost.dispose();
			}
		});
		btnCancel.setBounds(208, 319, 89, 23);
		frmHost.getContentPane().add(btnCancel);
		
		JButton btnHost = new JButton("Host");
		btnHost.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				AppGlobals.launcherConfig.enablePrority = chckbxEnablePriority.isSelected();
				AppGlobals.launcherConfig.disableCheckDeck = chckbxDisableCheckDeck.isSelected();
				AppGlobals.launcherConfig.disableShuffleDeck = chckbxDisableShuffleDeck.isSelected();
				AppGlobals.launcherConfig.lifepoints = textField.getText();
				Room room = new Room();
				room.banListType = comboBox.getSelectedIndex();
				room.rule = comboBox_1.getSelectedIndex();
				room.mode = comboBox_2.getSelectedIndex();
				room.timer = comboBox_3.getSelectedIndex();
				room.startLp = Integer.parseInt(textField.getText());
				room.isLocked = (textField_1.getText() != "") ? true : false;
				room.roomName = textField_1.getText();
				room.isRanked = rankedGame;
				ConfigHandler.saveCurrentConfig();
				if(room.roomName != "" && AppGlobals.roomList.get(room.roomName) == null){
					LauncherHelper.generateConfig(room);
					LauncherHelper.runGame("-j");
					frmHost.dispose();
					
				}else{
					GUIHandler.launchHostingFailedDialog();
				}
	
			}
		});
		btnHost.setBounds(109, 319, 89, 23);
		frmHost.getContentPane().add(btnHost);
	}
	
}
